<?php

Yii::import('zii.widgets.CMenu');

class EMenu extends CMenu
{
	public $menu_id;
	
	public function init()
	{
		$items = array();
		$criteria = new CDbCriteria();
		$criteria->condition = "disabled = 0";
		$menu = Menu::model()->with(array('menuItems'=>array('order'=>'menuItems.parent_id ASC, menuItems.sort ASC')))->findByPk($this->menu_id, $criteria);
		$route = $this->getController()->getRoute();
		
		$alias = $_SERVER['REQUEST_URI'];
		$pathInfoMain = UrlManager::removeLnSuffix(substr($alias, 1));
		$pathInfo = UrlManager::removeExSuffix($pathInfoMain);
		$alias = $pathInfo;
		foreach ($menu->menuItems as $key => $value){
			$url = $value->url[0]==='/' ? $value->url : '/'.$value->url;
			if($value->parent_id==0){
				$items[$value->id] = array('label'=>isset($value->ln->name) ? $value->ln->name : $value->name, 'url'=>'/'.Yii::app()->language.$url);// 'url'=>array($value->url));
				$items[$value->id]['id'] = $value->id;
				$items[$value->id]['active'] = $alias == $value->url || $route=='site/index' && $value->url=='/' ? true : false;
			} else {
				$items[$value->parent_id]['items'][] = array('label'=>isset($value->ln->name) ? $value->ln->name : $value->name, 'url'=>'/'.Yii::app()->language.$url, 'id' => $value->id);
			}
		}
		$this->items = $items;
		$this->htmlOptions=array('class'=>'d_in fl_l'); 
		$this->submenuHtmlOptions=array('class'=>'podmenu_header d_in'); 
		parent::init();
	}
	
	protected function renderMenuRecursive($items)
	{
		$count=0;
		$n=count($items);
		foreach($items as $item)
		{
			$count++;
			$options=isset($item['itemOptions']) ? $item['itemOptions'] : array();
			$class=array();
			if($item['active'] && $this->activeCssClass!='')
				$class[]=$this->activeCssClass;
			if($count===1 && $this->firstItemCssClass!==null)
				$class[]=$this->firstItemCssClass;
			if($count===$n && $this->lastItemCssClass!==null)
				$class[]=$this->lastItemCssClass;
			if($this->itemCssClass!==null)
				$class[]=$this->itemCssClass;
			if($class!==array())
			{
				if(empty($options['class']))
					$options['class']=implode(' ',$class);
				else
					$options['class'].=' '.implode(' ',$class);
			}
	
			echo CHtml::openTag('li', $options);
			
			$menu = $item['label']=='*' ? CHtml::tag('hr') : $this->renderMenuItem($item);
			if(isset($this->itemTemplate) || isset($item['template']))
			{
				$template = isset($item['template']) ? $item['template'] : $this->itemTemplate;
				echo strtr($template,array('{menu}'=>$menu));
			}
			else
				echo $menu;
	
			if(isset($item['items']) && count($item['items']))
			{
				echo "\n".CHtml::openTag('div',isset($item['submenuOptions']) ? $item['submenuOptions'] : $this->submenuHtmlOptions)."\n";
				echo "\n".CHtml::openTag('ul')."\n";
				$this->renderMenuRecursive($item['items']);
				echo CHtml::closeTag('ul')."\n";
				echo CHtml::closeTag('div')."\n";
			}
	
			echo CHtml::closeTag('li')."\n";
		}
	}
	protected function renderMenuItem($item)
	{
		if(isset($item['url']))
		{
			$item['linkOptions']['eid'] = $item['id'];
			$item['linkOptions']['ename'] = 'name';
			$item['linkOptions']['etype'] = 'text';
			$item['linkOptions']['etable'] = 'menuItems';
			$label=$this->linkLabelWrapper===null ? $item['label'] : '<'.$this->linkLabelWrapper.'>'.$item['label'].'</'.$this->linkLabelWrapper.'>';
			return CHtml::link($label,$item['url'],isset($item['linkOptions']) ? $item['linkOptions'] : array());
		}
		else
			return CHtml::tag('span',isset($item['linkOptions']) ? $item['linkOptions'] : array(), $item['label']);
	
	}
}